package com.darren.archive.exception;

import com.darren.archive.bean.ResponseBean;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * @Description: 全局异常处理器
 * @Author: 周金涛
 * @Date: 2019-09-19 12:38:41
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResponseBean handleException(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError("不支持' " + e.getMethod() + "'请求");
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseBean handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError("服务器错误，请联系管理员");
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(ArchiveException.class)
    public Object pmsException(HttpServletRequest request, ArchiveException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError(e.getMessage());

    }

    /**
     * Token 异常
     */
    @ExceptionHandler(TokenException.class)
    public ResponseBean tokenException(HttpServletRequest request, TokenException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError(e.getStatus(),e.getMessage());
    }

    /**
     * 登录时验证账号异常
     */
    @ExceptionHandler(UnknownAccountException.class)
    public Object unknownAccountException(HttpServletRequest request, UnknownAccountException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError("账号不存在！");

    }

    /**
     * 登录时验证密码异常
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Object incorrectCredentialsException(HttpServletRequest request, IncorrectCredentialsException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isError("密码不正确！");

    }

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseBean unauthorizedException(HttpServletRequest request, UnauthorizedException e) {
        log.error(e.getMessage(), e);
        return ResponseBean.isWarn("您无操作权限！");

    }

    /**
     * post请求参数校验抛出的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseBean methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        //获取异常中随机一个异常信息
        String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
        return ResponseBean.isError(defaultMessage);
    }

    /**
     * get请求参数校验抛出的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ResponseBean bindExceptionHandler(BindException e) {
        //获取异常中随机一个异常信息
        String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
        return ResponseBean.isError(defaultMessage);
    }

    /**
     * 请求方法中校验抛出的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseBean constraintViolationExceptionHandler(ConstraintViolationException e) {
        //获取异常中第一个错误信息
        String message = e.getConstraintViolations().iterator().next().getMessage();
        return ResponseBean.isError(message);
    }
}
